/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"5A5DBEEB-B2C1-4A25-A7E1-7EF436CBA902",variableType:4}
 */
var fCodArea = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"CF35B038-87E2-4295-A4EE-CB3536E6934A",variableType:4}
 */
var fTodos = 0;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"91CD4AFF-570F-497F-89C6-C17E1C9538CE"}
 */
var fProveedorHasta = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"BA94C736-B76D-4437-BF1F-A651A2F1A4CA"}
 */
var fProveedorDesde = null;

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"A4D0356B-B7B5-48C3-8D5C-D528585D138A"}
 */
function onDataChangeTodos(oldValue, newValue, event) {
	elements.fProveedorDesde.enabled = !newValue
	elements.fProveedorHasta.enabled = !newValue
	return true
}

/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"56EA3478-7B95-469C-95AB-0C7EAD66B58A"}
 */
function onDataChangeProvDesde(oldValue, newValue, event) {
	if (fProveedorHasta == null)
	{
		fProveedorHasta = newValue
	}
	return true
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"53AD86B4-24F2-44FB-A3AB-ED32B1B35828"}
 */
function onShow(firstShow, event) {
	fTodos = 0
	fProveedorDesde = null
	fProveedorHasta = null
	fCodArea		= null
	elements.fProveedorDesde.enabled = true
	elements.fProveedorHasta.enabled = true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"62274CCB-E6EF-4835-B21E-A1E9A373DB8B"}
 */
function buscar(event) {
	if (fCodArea == null)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor indique el area','OK')
		return
	}
	if (fTodos == 0 && (fProveedorDesde == null || fProveedorHasta == null))
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor indique el rango de proveedores','OK')
		return
	}
	
	var $query = 'select p.codigo_anterior, p.razon_social, SUM(importe) as "total" FROM ctacte_pr "cc" '+
					' JOIN proveedor "p" ON p.proveedor_id = cc.proveedor_id '+
					' JOIN tab_mov_pr "tb" ON cc.cod_mov = tb.ctacte_mov_pr_id '+
					' WHERE tb.diario_compras = 1 AND cc.fecha_mov BETWEEN ? and ? AND cc.cod_area = ? '
	/** @type {Array<Object>}*/
	var args = [globals.gFechaDesde, globals.gFechaHasta, fCodArea]
	if (fTodos == 0)
	{
		if (fProveedorDesde == fProveedorHasta)
		{
			$query += ' AND p.razon_social = ? '
			args.push(fProveedorDesde)
		}
		else
		{
			$query += ' AND p.razon_social BETWEEN ? and ? '
			args.push(fProveedorDesde)
			args.push(fProveedorHasta)
		}
	}
	$query += ' GROUP BY p.codigo_anterior, p.razon_social ORDER BY p.razon_social'
		
	var $ds = databaseManager.getDataSetByQuery('infocom',$query,args,-1)
	
	if ($ds.getMaxRowIndex() == 0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','No se encontraron resultados','OK')
		return
	}
	forms.rpt_proveedor_area_2.fCodArea = fCodArea
	var $frm = solutionModel.getForm('rpt_proveedor_area_2')
	var $tipos = [JSColumn.INTEGER, JSColumn.TEXT, JSColumn.NUMBER]
	$frm.dataSource = $ds.createDataSource('RPT_AREA',$tipos)
	forms[$frm.name].controller.recreateUI()
	forms.rpt_proveedor_area_2.pdf()
}
